package demo2.util.ai;

import demo2.parser.BinaryTree;
import demo2.util.Evaluator;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class BruteSearcher implements Searcher {

    public Set<String> searchAll(int[] arr) {
        Set<String> solutions = new TreeSet<>();

        for (int i = 0; i < 4; i++)
            for (int j = 0; j < 4; j++)
                for (int k = 0; k < 4; k++) {
                    String X = OPERATORS[i];
                    String Y = OPERATORS[j];
                    String Z = OPERATORS[k];
                    List<String> exp = new ArrayList<String>();
                    for (int num : arr) exp.add(num + "");
                    exp.add(X);
                    exp.add(Y);
                    exp.add(Z);

                    for (int cc = 0; cc < Canto.FAC[7]; cc++) {
                        int[] idx = Canto.codel(cc, 7);
                        List<String> list = new ArrayList<String>();
                        for (int jj = 0; jj < 7; jj++) {
                            list.add(exp.get(idx[jj]));
                        }
                        if (Math.abs(new Evaluator().eval(list) - GOAL) < EPS) {
                            BinaryTree tree = BinaryTree.creatTree(list);
                            StringBuffer buffer = new StringBuffer();
                            tree.midVisit(buffer);
                            solutions.add(buffer.toString());
                        }
                    }
                }
        return solutions;
    }

}
